<template>
  <div>
    <div>
      <van-nav-bar
        title="发送新闻"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      >
        <template #right>
          <van-button round @click="onClickRight" type="primary"
            >发送</van-button
          >
        </template>
      </van-nav-bar>
    </div>
    <!--  -->
    <div>
      <el-input placeholder="标题" v-model="title" style="width: 90%"></el-input>
      <textarea
        placeholder="请输入内容"
        v-model="text"
        name=""
        id=""
        cols="50"
        rows="10"
      ></textarea>
    </div>
    <!--  -->
    <div></div>
    <!--  -->
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      text: "",
      title: '',
    };
  },
  props: {},

  components: {},

  created() {},

  mounted() {},

  methods: {
    onClickLeft() {
      this.$router.push("/news");
    },
    // 发送新闻
    onClickRight() {
        if (!this.text || !this.title){
            this.$message({
              type: 'warning',
              message: '请输入要发送的内容',
            }); return;
        }
      this.axios
        .post("/news/add", { value: this.text, title: this.title, user_id: localStorage.user_id })
        .then((res) => {
          console.log("发送新闻的响应:", res);
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: res.data.message,
            });
            this.$router.push("/news");
          } else {
            this.$message({
              type: "error",
              message: res.data.message,
            });
          }
        })
        .catch((err) => {
          console.log("发送报错的响应:", err);
        });
    },
  },

  computed: {},

  watch: {},

  directives: {},

  filters: {},
};
</script>

<style scoped>
* {
  margin: 0;
}
</style>
